package com.polaris.live.adapter

import android.view.View
import com.bumptech.glide.Glide
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.databinding.AdapterHonorItemBinding
import com.polaris.live.resp.back_resp.UserMedalVo
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import org.libpag.PAGFile
import java.io.File

/**
 * HonorViewAdapter
 * @author Created by 天晴 on 2024/1/26 10:36
 * @since 1.0.0
 **/
class HonorViewAdapter : BaseBindingAdapter<AdapterHonorItemBinding, UserMedalVo>() {
    override fun convert(holder: VBViewHolder<AdapterHonorItemBinding>, item: UserMedalVo) {
        holder.vb.apply {
            imageIcon.visibility = View.GONE
            pagView.visibility = View.GONE
            if (item.createTime == null) {
                imageIcon.visibility = View.VISIBLE
                Glide.with(imageIcon).load(ImageUtils.imgPath(item.fallbackPhotoUrl))
                    .into(imageIcon)
            } else {
                if (item.pagUrl?.isNotEmpty() == true) {
                    pagView.visibility = View.VISIBLE
                    val path = FileUtils.getFilePath(FileUtils.getPagFile(), item.pagUrl)
                    if (File(path).exists()) {
                        pagView.apply {
                            setRepeatCount(-1)
                            composition = PAGFile.Load(path)
                            play()
                        }
                    }
                } else {
                    imageIcon.visibility = View.VISIBLE
                    Glide.with(imageIcon).load(ImageUtils.imgPath(item.photoUrl)).into(imageIcon)
                }
            }
        }
    }

}